package com.yupi.oj.controller;

import com.yupi.oj.common.BaseResponse;
import com.yupi.oj.common.ErrorCode;
import com.yupi.oj.common.ResultUtils;
import com.yupi.oj.exception.ThrowUtils;
import com.yupi.oj.model.dto.ai.AiMessage;
import com.yupi.oj.model.dto.ai.AiRequest;
import com.yupi.oj.service.AiAnswerService;
import com.yupi.oj.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * ai接口
 */
@RestController
@RequestMapping("/ai")
@Slf4j
public class AiController {
    @Autowired
    private AiAnswerService aiAnswerService;

    /**
     * AI回答用户的单条信息
     * @param aiRequest
     * @param request
     * @return
     */
    @PostMapping("/do_chat")
    public BaseResponse<String> doChat(@RequestBody AiRequest aiRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(aiRequest == null, ErrorCode.PARAMS_ERROR);
        String aiMessages = aiAnswerService.doChat(aiRequest, request);
        return ResultUtils.success(aiMessages);
    }

    /**
     * 根据题目Id与用户Id查询对话列表
     * @param questionId
     * @param request
     * @return
     */
    @GetMapping("/get/by/question_id")
    public BaseResponse<List<AiMessage>> getAiMessageByQuestionId(@RequestParam Long questionId,HttpServletRequest request) {
        ThrowUtils.throwIf(questionId == null || questionId < 0, ErrorCode.PARAMS_ERROR);
        List<AiMessage> aiMessages = aiAnswerService.getAiMessageByQuestionId(questionId,request);
        return ResultUtils.success(aiMessages);
    }
    @GetMapping("/sse/do_chat_stream")
    public SseEmitter doChatStream(AiRequest aiRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(aiRequest == null, ErrorCode.PARAMS_ERROR);
        SseEmitter sseEmitter = aiAnswerService.doChatStream(aiRequest, request);
        return sseEmitter;
    }
}
